import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/Home/home.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: () => import('../views/Welcome.vue')
      },
      {
        path: '/users',
        component: () => import('../views/User/user.vue')
      },
      {
        path: '/rights',
        component: () => import('../views/Power/rights.vue')
      },
      {
        path: '/roles',
        component: () => import('../views/Power/roles.vue')
      },
      {
        path: '/categories',
        component: () => import('../views/goods/cate.vue')
      },
      {
        path: '/params',
        component: () => import('../views/goods/params.vue')
      },
      {
        path: '/goods',
        component: () => import('../views/goods/goods.vue')
      },
      {
        path: '/goods/add',
        component: () => import('../views/goods/add.vue')
      },
      {
        path: '/orders',
        component: () => import('../views/order/order.vue')
      },
      {
        path: '/reports',
        component: () => import('../views/report/report.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login/login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 拦截路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
